import React from 'react';
import PropTypes from 'prop-types';
import EditForm from './edit-form'
import { stripesConnect } from '@folio/stripes/core';
import { injectIntl,FormattedMessage } from 'react-intl';
class formSachet extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            operator: {},
            editParams:{}
        };
    }
    static manifest = Object.freeze({
        httpRulesInvoke: {
          type: 'okapi',
          accumulate: true,
          path: () => 'party/rules'
        },
        httpRulesModifyInvoke: {
          type: 'okapi',
          accumulate: true,
          path: () => 'party/rules/modify'
        }
      });
    static propTypes = {
        mutator: PropTypes.shape({
            httpRulesInvoke: PropTypes.shape({
            PUT: PropTypes.func.isRequired,
            POST: PropTypes.func.isRequired,
            GET: PropTypes.func.isRequired,
            DELETE: PropTypes.func.isRequired
          }) ,
          httpRulesModifyInvoke: PropTypes.shape({
            PUT: PropTypes.func.isRequired,
            POST: PropTypes.func.isRequired,
            GET: PropTypes.func.isRequired,
            DELETE: PropTypes.func.isRequired
          }) 
        }).isRequired,
        name:PropTypes.string,
        okapi:PropTypes.object,
        params:PropTypes.object,
        removeTopMenu:PropTypes.func,
        matchPath:PropTypes.string,
        toModel:PropTypes.func
      };  
      componentDidMount() {
    
        this.initData();
        // console.log(this.props);
        const { currentUser } = this.props.okapi;
        if (currentUser) {
          this.setState({
            operator: currentUser
          });
        }
      } 
      initData = () => {
        if (
    
          this.props.params &&
          this.props.params.id
        ) {
          const params = this.props.params;
    
          const { GET } = this.props.mutator.httpRulesInvoke;
          GET({ path: `party/rules/modify/${params.id}` }).then(res => {
            this.setState({
              editParams: res
            });
          });
        }else{
            this.setState({
                editParams: {
                    absence:1,
                    leave:1,
                    late:1,
                    leaveEarly:1,
                    prisonTermDay:1
                }
              });  
        }
      };
       cancel = (flag = true) => {
        const { removeTopMenu, matchPath } = this.props;
        removeTopMenu(matchPath, flag);
      };
        editCommit = ({ requestPreferences, creds, ...formData }) => {
          formData.operator = this.state.operator;
          delete formData.operator.addresses;
          delete formData.operator.preferredContactTypeId;
          delete formData.operator.servicePoints;
          delete formData.operator.curServicePoint;
          delete formData.operator.dateOfBirth;
          if (formData && formData.id) {
            this.update(formData);
          } else if (formData && !formData.id) {
            this.add(formData);
          }
      }
      update=(values)=>{
        const {
          intl,
          mutator,
          history
        } = this.props;
        const { PUT } = this.props.mutator.httpRulesModifyInvoke;
        PUT(values).then(response => {
          alert(intl.formatMessage({id:"ui-party.other.saved.successfully"}));
          this.cancel(false);
        });
      }
      add=(values)=>{
        const {
          intl,
          mutator,
          history
        } = this.props;
        const { POST } = this.props.mutator.httpRulesInvoke;
     
        POST(values).then(res=>{
        
          alert(intl.formatMessage({id:"ui-party.other.saved.successfully"}));
          this.cancel(false);
        })
      }
    render() {
        return (
      <EditForm
        onSubmit={this.editCommit}
        initialValues={this.state.editParams}
        cancel={this.cancel}
        httpValidatorInvoke={this.props.mutator.httpRulesInvoke}
        toModel={this.props.toModel}
        removeTopMenu={this.props.removeTopMenu}
        form={this.props.name}
      />
        );
    }
}

 

export default injectIntl(stripesConnect(formSachet));
